package com.djl.rabbitmq.three;

import com.djl.rabbitmq.utils.RabbitMqUtils;
import com.djl.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

/**
 * @program: rabbitmq-hello
 * @description: 消费者
 * @author: Mr.dai
 * @create: 2021-09-10 14:25
 **/

public class Worker01 {
    private static final String task_queue_name="ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("c1等待接收消息处理——时间较短");
    //  采用手动应答
        DeliverCallback deliverCallback=( consumerTag,  message)->{
            //沉睡一秒
            SleepUtils.sleep(1);
            System.out.println("接收到消息："+new String(message.getBody(),"UTF-8"));
        //    手动应答
            /**
             * 1.消息的标记
             * 2.是否批量应答 false表示不批量应答信道中的消息
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        //设置不公平分发
        //欲取值
        int prefetchCount=5;
        channel.basicQos(prefetchCount);
        boolean autoAck=false;
        channel.basicConsume(task_queue_name,autoAck,deliverCallback,(consumerTag) ->{
            System.out.println(consumerTag+"消费者取消消费接口回调逻辑");
        });


    }
}
